﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace T15_事件的定义与事件订阅者
{
    class Program
    {
        static void Main(string[] args) 
        {
            Eventbuy a = new Eventbuy();
            Mrwang wang = new Mrwang();
            Mrzhang zhang = new Mrzhang();
            //注册
            wang.registe(a);
            zhang.registe(a);
            //发布
            a.issue();
            Console.ReadKey();


        }
    }
    class Eventbuy
    {
        public delegate void Buyhandle();//定义委托
        public event Buyhandle buy;//定义事件
        public void issue()
        {
            if (buy != null)
            {
                Console.WriteLine("水果到了");
                buy();//触发事件
            }

        }
    }

    class Mrwang
    {
        public void recieve()
        {
            Console.WriteLine("接到消息，王要卖水果");
        }
        public void registe(Eventbuy e)//注册事件
        {
            e.buy += recieve;
        }
    }
    class Mrzhang
    {
        public void recieve()
        {
            Console.WriteLine("接到消息，张要卖水果");
        }
        public void registe(Eventbuy e)//注册事件
        {
            e.buy += recieve;
        }
    }
 
}